package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    private static final String CART = "Cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /**
     * 查询购物车列表数据
     *
     * @param username 登录人
     * @return
     */
    @Override
    public Map list(String username) {
        Map map = new HashMap();

        // 从redis中查询列表数据
        List<OrderItem> orderItemLists = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList", orderItemLists);

        // 商品总数量，总价格
        Integer totalNum = 0;
        Integer totalMoney = 0;

        for (OrderItem orderItem : orderItemLists) {
            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();  // 总价格
        }
        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);

        return map;
    }

    /**
     * 添加商品到购物车
     *
     * @param skuId    sku商品id
     * @param num      商品数量
     * @param username 登录人名称
     */
    @Override
    public void addCart(String skuId, Integer num, String username) {
        // 1. 查询redis中对应的商品信息orderItem
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
        // 2. 如果redis中已经有了，则追加数量，重新计算金额
        if (orderItem != null) {
            // 更新数量
            orderItem.setNum(orderItem.getNum() + num);

            // 判断数量是否为0,为0删除该商品
            if (orderItem.getNum() <= 0) {
                // 删除该商品
                redisTemplate.boundHashOps(CART + username).delete(skuId);
                return;
            }

            // 更新金额
            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            // 支付金额
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
        } else {
            // 3. 如果没有，将商品添加到缓存
            Result<Sku> skuResult = skuFeign.findById(skuId);
            Sku sku = skuResult.getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();

            // 将SKU转换成OrderItem
            orderItem = this.sku2OrderItem(sku, spu, num);
        }
        // 存入redis
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
    }

    // sku转换为orderItem
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * orderItem.getPrice());  // 多个数量商品金额：单价*数量
        orderItem.setPayMoney(num * orderItem.getPrice());  // 实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);  // 重量=单个重量*数量

        // 分类id设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
